package com.g4.shoppingback.controller;


import com.g4.shoppingback.annotation.WriteLog;
import com.g4.shoppingback.common.Result;
import com.g4.shoppingback.pojo.DdKindType;
import com.g4.shoppingback.service.IDdKindTypeService;
import com.g4.shoppingback.common.PageVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 型号列表 前端控制器
 * </p>
 *
 * @author admin
 * @since 2022-02-19
 */
@RestController
@CrossOrigin
@RequestMapping("/shoppingback/ddKindType")
public class DdKindTypeController {

    @Autowired
    private IDdKindTypeService iDdKindTypeService;

    //商品详情添加
    @WriteLog("新增商品配置")
    @PreAuthorize("hasAnyAuthority('miShop:kind:add')")
    @PostMapping
    public Result addKind(@RequestBody DdKindType ddKindType){

        //传入一个商品配置类对象
        iDdKindTypeService.addPd(ddKindType);
        return new Result(true, "配置添加成功,新添加配置默认为禁售状态!");
    }

    //删除商品类别
    @WriteLog("删除商品配置")
    @PreAuthorize("hasAnyAuthority('miShop:kind:delete')")
    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable Integer id){

        //传入配置表对象信息,在商品配置的业务层执行 delete (删除)方法
        iDdKindTypeService.deleteById(id);

        return Result.success("操作成功");
    }

    //修改商品配置信息
    @WriteLog("修改商品配置")
    @PreAuthorize("hasAnyAuthority('miShop:kind:update')")
    @PutMapping("/updateSta")
    public Result updateSta(@RequestBody DdKindType ddKindType) {

        iDdKindTypeService.updateSta(ddKindType);

        return new Result(true, "商品配置修改成功");
    }


    //根据传入id 对商品配置 的状态进行修改
    @WriteLog("修改配置售卖状态")
    @PreAuthorize("hasAnyAuthority('miShop:kind:status')")
    @PostMapping("/updateItem/{id}/{state}")
    public Result updateItem(@PathVariable Integer id,
                             @PathVariable Integer state) {

        System.out.println(id);
        System.out.println(state);
        iDdKindTypeService.updateItem(id,state);

        return new Result(true, "商品配置修改成功");
    }


    //分页查询商品配置
    @PreAuthorize("hasAnyAuthority('miShop:kind:find')")
    @GetMapping
    public Result getKindType(PageVo pageVo){

        pageVo = iDdKindTypeService.getKindType(pageVo);
        return Result.ok(pageVo);
    }

    //根据id查询配置
    @GetMapping("/{id}")
    public Result selectById(@PathVariable Integer id){

        DdKindType ddKindType= iDdKindTypeService.selectById(id);

        return Result.ok(ddKindType);
    }
}
